package com.chinaisafe.user.controller;

import com.chinaisafe.base.common.validated.AddGroup;
import com.chinaisafe.user.biz.SysMenuBiz;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.chinaisafe.base.common.annotation.OperationLog;
import com.chinaisafe.base.common.validated.UpdateGroup;
import com.chinaisafe.base.common.enums.BusinessTypeEnum;
import com.chinaisafe.base.common.vo.Answer;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.chinaisafe.user.vo.request.SysMenuReqVO;
import com.chinaisafe.user.vo.response.SysMenuRespVO;
import org.springframework.validation.annotation.Validated;
import com.chinaisafe.user.service.SysMenuService;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * SysMenuController
 * Description：菜单(资源)接口层
 *
 * @author tby
 * @date  2024-07-11 17:32:41
 */
@RestController
@RequestMapping("/sysMenu")
@Tag(name = "菜单(资源)接口层", description = "菜单(资源)接口层")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @Resource
    private SysMenuBiz sysMenuBiz;

    @GetMapping("/pageList")
    @Operation(summary = "获取菜单树", description = "获取菜单树")
    public Answer<List<SysMenuRespVO>> pageList(SysMenuReqVO sysMenuReqVO) {
        List<SysMenuRespVO> result = sysMenuService.getList(sysMenuReqVO);
        return Answer.success("获取成功", result);
    }

    @PostMapping()
    @Operation(summary = "新增菜单(资源)", description = "新增菜单(资源)")
    @OperationLog(title = "菜单(资源)-新增", businessType = BusinessTypeEnum.INSERT)
    public Answer<SysMenuRespVO> add(@RequestBody @Validated(AddGroup.class) SysMenuReqVO sysMenuReqVO) {
        SysMenuRespVO sysMenuRespVO = sysMenuService.add(sysMenuReqVO);
        return Answer.success("保存成功", sysMenuRespVO);
    }

    @PutMapping()
    @Operation(summary = "根据id编辑菜单(资源)", description = "根据id编辑菜单(资源)")
    @OperationLog(title = "菜单(资源)-根据id编辑", businessType = BusinessTypeEnum.UPDATE)
    public Answer<SysMenuRespVO> update(@RequestBody @Validated(UpdateGroup.class) SysMenuReqVO sysMenuReqVO) {
        SysMenuRespVO sysMenuRespVO = sysMenuBiz.update(sysMenuReqVO);
        return Answer.success("修改成功", sysMenuRespVO);
    }

    @GetMapping("/{sysMenuId}")
    @Operation(summary = "根据id获取菜单(资源)", description = "根据id获取菜单(资源)")
    public Answer<SysMenuRespVO> getDetailById(@PathVariable Long sysMenuId) {
        SysMenuRespVO sysMenuRespVO = sysMenuBiz.getDetailById(sysMenuId);
        return Answer.success("获取成功", sysMenuRespVO);
    }

    @PostMapping("enableSwitch")
    @Operation(summary = "菜单启用/禁用", description = "参数请放在?后面")
    public Answer<Void> enableSwitch(Long id) {
        sysMenuBiz.enableSwitch(id);
        return Answer.success("操作成功");
    }

    @DeleteMapping()
    @Operation(summary = "根据id删除菜单(资源)", description = "根据id删除菜单(资源)")
    @OperationLog(title = "菜单(资源)-根据id删除", businessType = BusinessTypeEnum.DELETE)
    public Answer<Void> deleteByIds(@RequestParam("ids") Long[] ids) {
        sysMenuService.deleteByIds(ids);
        return Answer.success("删除成功");
    }
}

